package com.zhiyou100.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("file")
public class FileController {
	@RequestMapping("upload.do")
	// 拿到jsp传来的值 上传文件属性
	public String file(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
		// 要保存的路径
		// String path = "D:\\springmvcimg\\";
		//获取当前工程的真实路径  上传在image下
		String path = request.getServletContext().getRealPath("image/");
		// 根据文件本身的名称
		String fileName = "";
		fileName += file.getOriginalFilename();
		// 为了防止 文件名相同 加上时间戳
		fileName += System.currentTimeMillis();
		// 再加上session的id
		fileName += request.getSession().getId();
		// 还觉得不行可以加上一个随机数
		// fileName += (int) (Math.random() * 1000);
		try {
			// 上传到
			file.transferTo(new File(path + fileName));
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "***";
	}
}
